package edu.nus.mtech.sem1.ca.main;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Set;
import java.util.regex.Pattern;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import edu.nus.mtech.sem1.ca.auth.UserRole;
import edu.nus.mtech.sem1.ca.cust.Customer;
import edu.nus.mtech.sem1.ca.cust.Status;
import edu.nus.mtech.sem1.ca.data.Database;
import edu.nus.mtech.sem1.ca.data.DatabaseLoader;
import edu.nus.mtech.sem1.ca.exception.CustomerNotFoundException;
import edu.nus.mtech.sem1.ca.main.dao.TelephonyApplicationDao;
import edu.nus.mtech.sem1.ca.plan.Account;
import edu.nus.mtech.sem1.ca.plan.Plan;
import edu.nus.mtech.sem1.ca.plan.PlanType;
import edu.nus.mtech.sem1.ca.plan.valueadd.ValueAddedService;
import edu.nus.mtech.sem1.ca.plan.valueadd.ValueAddedType;
import edu.nus.mtech.sem1.ca.utils.Common;
import edu.nus.mtech.sem1.ca.utils.DateInputControl;
import edu.nus.mtech.sem1.ca.utils.Logger;
import edu.nus.mtech.sem1.ca.utils.TelephonyConstants;
import edu.nus.mtech.sem1.ca.utils.TelephonyUtils;
/**
 * Single page form for customer
 *
 */
public class FormCustomer extends JInternalFrame {

	private JPanel contentPane;
	private JTextField txtName;
	private DateInputControl jTextField_roaming_end;
	private JCheckBox jCheckBox_Data_mobile;
	private JCheckBox jCheckBox_roaming;
	private JCheckBox jCheckBox_Mobile_IDD;
	private JCheckBox jCheckBox_Call_transfer;
	private JCheckBox jCheckBox2;
	private DateInputControl jTextField_mobileData_start;
	private JComboBox jComboBox_TotalChannel2addModel;
	private DateInputControl jTextField_mobileData_end;
	private DateInputControl jTextField_mobileIDD_end;
	private DateInputControl jTextField_roaming_start;
	private DateInputControl jTextField_mobileIDD_start;
	private JLabel jLabel_valueAdded_date_terminated;
	private JLabel jLabel_valueAdd_date_commence;
	private DateInputControl jTextField_callXsfer_terminated;
	private DateInputControl jTextField_voiceIDD_terminated;
	private JLabel jLabel_call_transfer;
	private JLabel jLabel_voiceIDD_dateTerminated;
	private JTextField jTextField_callXfer;
	private JLabel jLabel_callXsfer;
	private DateInputControl jTextField_voiceIDD_date_commenced;
	private JLabel jLabel_voiceIDD;
	private JLabel jLabel_channel;
	private JCheckBox jCheckBox_Value_add_IDD;
	private JTextField txtNRIC;
	private JTextField txtEmail;
	private JTextField txtAddress;
	private JTextField txtAccountNo;
	private JCheckBox chkStatus;
	private Customer mCustomer=new Customer();
	
	private JTextField txtInterests;
	private JTextField txtDVnumber;
	private JTextField txtMVnumber;
	private DateInputControl dteMVCommence;
	private DateInputControl dteMVTerminated;
	private DateInputControl dteDVcommenced;
	private DateInputControl dteDVterminated;
	private DateInputControl dteCTcommenced;
	private DateInputControl dteCTterminated;
	private JButton btnGenerateBill;
	private JCheckBox chkDVstatus;
	private JCheckBox chkMVstatus;
	private JCheckBox chkCTstatus;	
	private ComboBoxModel jComboBox_TotalChannel2addModelModel;

	private Plan mDVPlan;
	private Plan mMobilePlan;
	private Plan mCablePlan;

	private TelephonyApplicationDao dao=new TelephonyApplicationDao();
	private JButton btnSearchCustomers;
	private JButton btnViewComplaints;
	private JButton btnCreateComplaint;

	//Needed the constructor if the request is coming from the Search table 
	public FormCustomer(Customer customer){
		
		super("Customer Information of "+customer.getName(), 
	              false, //resizable
	              true, //closable
	              false, //maximizable
	              true);
		
		initialize();
		
		try {
			searchCustomer(customer.getNric());
			searchSubscription(customer);			
		} catch (CustomerNotFoundException e) {
			e.printStackTrace();
		}
		
		
		
	}
	
	/**
	 * Create the frame.
	 */
	public FormCustomer() {		
		super("Customer Information", 
	              false, //resizable
	              true, //closable
	              false, //maximizable
	              true);
		
		
		initialize();
		btnGenerateBill.setEnabled(false);	
	}

	private void initialize() {
		
		this.setBounds(0, 0, 789, 543);
		this.setPreferredSize(new java.awt.Dimension(714, 543));
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panelCustomerInfo = new JPanel();
		panelCustomerInfo.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Customer Information", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panelCustomerInfo.setBounds(10, 11, 753, 174);
		contentPane.add(panelCustomerInfo);
		panelCustomerInfo.setLayout(null);
		
		JLabel lblName = new JLabel("Name");
		lblName.setBounds(13, 59, 75, 14);
		panelCustomerInfo.add(lblName);
		
		JLabel lblNric = new JLabel("NRIC");
		lblNric.setBounds(13, 27, 75, 14);
		panelCustomerInfo.add(lblNric);
		
		JLabel lblAddress = new JLabel("Address");
		lblAddress.setBounds(13, 91, 75, 14);
		panelCustomerInfo.add(lblAddress);
		
		JLabel lblEmail = new JLabel("Email");
		lblEmail.setHorizontalAlignment(SwingConstants.LEFT);
		lblEmail.setBounds(295, 59, 46, 14);
		panelCustomerInfo.add(lblEmail);
		
		txtName = new JTextField();
		txtName.setBounds(98, 56, 187, 20);
		panelCustomerInfo.add(txtName);
		txtName.setColumns(10);
		
		txtNRIC = new JTextField();
		txtNRIC.setBounds(98, 24, 187, 20);
		panelCustomerInfo.add(txtNRIC);
		txtNRIC.setColumns(10);
		
		txtEmail = new JTextField();
		txtEmail.setColumns(10);
		txtEmail.setBounds(369, 56, 180, 20);
		panelCustomerInfo.add(txtEmail);
		
		txtAddress = new JTextField();
		txtAddress.setColumns(10);
		txtAddress.setBounds(98, 88, 451, 20);
		panelCustomerInfo.add(txtAddress);
		
		txtAccountNo = new JTextField();
		txtAccountNo.setBounds(369, 24, 180, 20);
		panelCustomerInfo.add(txtAccountNo);
		
		JLabel lblAcctno = new JLabel("Account No.");
		lblAcctno.setHorizontalAlignment(SwingConstants.LEFT);
		lblAcctno.setBounds(295, 27, 75, 14);
		panelCustomerInfo.add(lblAcctno);
		
		JLabel lblStatus = new JLabel("Status");
		lblStatus.setBounds(13, 148, 46, 14);
		panelCustomerInfo.add(lblStatus);
		
		chkStatus = new JCheckBox("");
		chkStatus.setBounds(94, 144, 97, 23);
		panelCustomerInfo.add(chkStatus);
		
		JLabel lblInterest = new JLabel("Interests");
		lblInterest.setBounds(13, 120, 75, 14);
		panelCustomerInfo.add(lblInterest);
		
		txtInterests = new JTextField();
		txtInterests.setColumns(10);
		txtInterests.setBounds(98, 117, 451, 20);
		panelCustomerInfo.add(txtInterests);
		
		btnCreateComplaint = new JButton("Create Complaint");
		btnCreateComplaint.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionEvent) {
				if(!txtNRIC.getText().trim().equals("")){
					try {
						FormComplaint frmComplaintObj=new FormComplaint(txtNRIC.getText(),txtName.getText());
						getParent().getParent().add(frmComplaintObj);
						Common.OpenCenterScreen(frmComplaintObj, true);
					} catch (CustomerNotFoundException e) {
						// TODO: handle exception
						JOptionPane.showMessageDialog(null,e.getMessage(), "Info", JOptionPane.INFORMATION_MESSAGE);
					}
					
				}
			}
		});
		btnCreateComplaint.setBounds(601, 23, 142, 23);
		panelCustomerInfo.add(btnCreateComplaint);
		
		btnGenerateBill = new JButton("Generate Bill");
		btnGenerateBill.addActionListener(new BillSelectActionListener());
		btnGenerateBill.setBounds(601, 55, 142, 23);
		panelCustomerInfo.add(btnGenerateBill);
		
		JPanel panelSubscription = new JPanel();
		panelSubscription.setBorder(new TitledBorder(null, "Subscriptions", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panelSubscription.setLayout(null);
		panelSubscription.setBounds(10, 187, 753, 276);
		contentPane.add(panelSubscription);
		
JPanel tab1=new JPanel();
		JPanel tab2 =new JPanel();

		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.addTab("Digital Voice",tab1);
		tab1.setLayout(null);
		tab1.setPreferredSize(new java.awt.Dimension(660, 173));
		tab1.setSize(658, 184);

		JLabel lblDVstatus = new JLabel("Active");
		lblDVstatus.setBounds(10, 8, 48, 14);
		tab1.add(lblDVstatus);
		
		/*final JCheckBox*/ chkDVstatus = new JCheckBox("");
		chkDVstatus.setBounds(64, 4, 21, 21);
		tab1.add(chkDVstatus);
		
		JLabel lblAssignedNo = new JLabel("Assigned No.");
		lblAssignedNo.setBounds(10, 32, 75, 14);
		tab1.add(lblAssignedNo);
		
		txtDVnumber = new JTextField();
		txtDVnumber.setColumns(10);
		txtDVnumber.setBounds(136, 30, 116, 20);
		tab1.add(txtDVnumber);
		
		JLabel label_3 = new JLabel("Date commenced");
		label_3.setBounds(10, 58, 116, 14);
		tab1.add(label_3);
		
		dteDVcommenced = new DateInputControl();
		dteDVcommenced.setColumns(10);
		dteDVcommenced.setBounds(136, 55, 116, 20);
		tab1.add(dteDVcommenced);
		
		JLabel lblDateTerminated = new JLabel("Date Terminated");
		lblDateTerminated.setHorizontalAlignment(SwingConstants.RIGHT);
		lblDateTerminated.setBounds(283, 57, 100, 14);
		tab1.add(lblDateTerminated);
		
		dteDVterminated = new DateInputControl();
		dteDVterminated.setColumns(10);
		dteDVterminated.setBounds(390, 53, 114, 20);
		tab1.add(dteDVterminated);
		{
			jCheckBox_Value_add_IDD = new JCheckBox();
			tab1.add(jCheckBox_Value_add_IDD);
			jCheckBox_Value_add_IDD.setText("Voice IDD");
			jCheckBox_Value_add_IDD.setBounds(12, 85, 96, 18);
		}
		{
			jCheckBox_Call_transfer = new JCheckBox();
			tab1.add(jCheckBox_Call_transfer);
			jCheckBox_Call_transfer.setText("Call Transfer");
			jCheckBox_Call_transfer.setBounds(12, 130, 121, 18);
		}
		{
			jLabel_voiceIDD = new JLabel();
			tab1.add(jLabel_voiceIDD);
			jLabel_voiceIDD.setText("Date commenced");
			jLabel_voiceIDD.setBounds(12, 105, 104, 14);
		}
		{
			jTextField_voiceIDD_date_commenced = new DateInputControl();
			tab1.add(jTextField_voiceIDD_date_commenced);
			jTextField_voiceIDD_date_commenced.setBounds(136, 102, 116, 21);
		}
		{
			jLabel_callXsfer = new JLabel();
			tab1.add(jLabel_callXsfer);
			jLabel_callXsfer.setText("Date commenced");
			jLabel_callXsfer.setBounds(12, 151, 121, 14);
		}
		{
			jTextField_callXfer = new DateInputControl();
			tab1.add(jTextField_callXfer);
			jTextField_callXfer.setBounds(136, 148, 116, 21);
		}
		{
			jLabel_voiceIDD_dateTerminated = new JLabel();
			jLabel_voiceIDD_dateTerminated.setHorizontalAlignment(SwingConstants.RIGHT);
			tab1.add(jLabel_voiceIDD_dateTerminated);
			jLabel_voiceIDD_dateTerminated.setText("Date Terminated");
			jLabel_voiceIDD_dateTerminated.setBounds(283, 105, 100, 14);
		}
		{
			jLabel_call_transfer = new JLabel();
			jLabel_call_transfer.setHorizontalAlignment(SwingConstants.RIGHT);
			tab1.add(jLabel_call_transfer);
			jLabel_call_transfer.setText("Date Terminated");
			jLabel_call_transfer.setBounds(283, 152, 100, 14);
		}
		{
			jTextField_voiceIDD_terminated = new DateInputControl();
			tab1.add(jTextField_voiceIDD_terminated);
			jTextField_voiceIDD_terminated.setBounds(390, 102, 114, 21);
		}
		{
			jTextField_callXsfer_terminated = new DateInputControl();
			tab1.add(jTextField_callXsfer_terminated);
			jTextField_callXsfer_terminated.setBounds(390, 149, 114, 21);
		}
		
		tabbedPane.addTab("Mobile Voice",tab2);
		tab2.setLayout(null);
		
			JPanel tab3=new JPanel();
			tabbedPane.addTab("Cable TV", null, tab3, null);
			tab3.setLayout(null);
			{
				JLabel lblCTstatus = new JLabel("Active");
				tab3.add(lblCTstatus);
				lblCTstatus.setBounds(10, 10, 54, 14);
			}
			
				/*final JCheckBox*/ chkCTstatus = new JCheckBox("");
				tab3.add(chkCTstatus);
				chkCTstatus.setBounds(64, 7, 21, 21);
			
			{
				JLabel label_2 = new JLabel("Date commenced");
				tab3.add(label_2);
				label_2.setBounds(10, 35, 116, 14);
			}
			{
				dteCTcommenced = new DateInputControl();
				tab3.add(dteCTcommenced);
				dteCTcommenced.setColumns(10);
				dteCTcommenced.setBounds(136, 32, 116, 20);
			}
			{
				JLabel lblDateTerminated_2 = new JLabel("Date terminated");
				tab3.add(lblDateTerminated_2);
				lblDateTerminated_2.setHorizontalAlignment(SwingConstants.RIGHT);
				lblDateTerminated_2.setBounds(266, 37, 116, 14);
			}
			{
				dteCTterminated = new DateInputControl();
				tab3.add(dteCTterminated);
				dteCTterminated.setColumns(10);
				dteCTterminated.setBounds(392, 33, 116, 20);
			}
			{
				jLabel_channel = new JLabel();
				tab3.add(jLabel_channel);
				jLabel_channel.setText("Total Number of Channel to add :");
				jLabel_channel.setBounds(10, 97, 242, 14);
			}
			{
				/*ComboBoxModel*/ jComboBox_TotalChannel2addModelModel = 
					/*new DefaultComboBoxModel(
							new String[] { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"});*/
					new DefaultComboBoxModel(
							new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"});
				jComboBox_TotalChannel2addModel = new JComboBox();
				tab3.add(jComboBox_TotalChannel2addModel);
				jComboBox_TotalChannel2addModel.setModel(jComboBox_TotalChannel2addModelModel);
				jComboBox_TotalChannel2addModel.setBounds(305, 94, 87, 21);
			}

		JLabel lblMVstatus = new JLabel("Active");
		lblMVstatus.setBounds(10, 10, 48, 14);
		tab2.add(lblMVstatus);
		
		/*final JCheckBox*/ chkMVstatus = new JCheckBox("");
		chkMVstatus.setBounds(64, 7, 21, 21);
		tab2.add(chkMVstatus);
		
		JLabel label = new JLabel("Assigned No.");
		label.setBounds(10, 32, 75, 14);
		tab2.add(label);
		
		txtMVnumber = new JTextField();
		txtMVnumber.setColumns(10);
		txtMVnumber.setBounds(136, 31, 116, 20);
		tab2.add(txtMVnumber);
		
		JLabel lblDateCommenced = new JLabel("Date commenced");
		lblDateCommenced.setBounds(10, 61, 116, 14);
		tab2.add(lblDateCommenced);
		
		dteMVCommence = new DateInputControl();
		dteMVCommence.setColumns(10);
		dteMVCommence.setBounds(136, 56, 116, 20);
		tab2.add(dteMVCommence);
		
		dteMVTerminated = new DateInputControl();
		dteMVTerminated.setColumns(10);
		dteMVTerminated.setBounds(392, 56, 116, 20);
		tab2.add(dteMVTerminated);
		
		JLabel lblDateTerminated_1 = new JLabel("Date terminated");
		lblDateTerminated_1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblDateTerminated_1.setBounds(266, 61, 116, 14);
		tab2.add(lblDateTerminated_1);
		{
			jCheckBox_Mobile_IDD = new JCheckBox();
			tab2.add(jCheckBox_Mobile_IDD);
			jCheckBox_Mobile_IDD.setText("Mobile IDD");
			jCheckBox_Mobile_IDD.setBounds(10, 98, 146, 18);
		}
		{
			jCheckBox_Data_mobile = new JCheckBox();
			tab2.add(jCheckBox_Data_mobile);
			jCheckBox_Data_mobile.setText("Mobile Data Service");
			jCheckBox_Data_mobile.setBounds(10, 123, 146, 18);
		}
		{
			jCheckBox_roaming = new JCheckBox();
			tab2.add(jCheckBox_roaming);
			jCheckBox_roaming.setText("Roaming Calls");
			jCheckBox_roaming.setBounds(10, 148, 123, 18);
		}
		{
			jLabel_valueAdd_date_commence = new JLabel();
			tab2.add(jLabel_valueAdd_date_commence);
			jLabel_valueAdd_date_commence.setText("Date commenced");
			jLabel_valueAdd_date_commence.setBounds(252, 83, 106, 14);
		}
		{
			jLabel_valueAdded_date_terminated = new JLabel();
			tab2.add(jLabel_valueAdded_date_terminated);
			jLabel_valueAdded_date_terminated.setText("Date Terminated");
			jLabel_valueAdded_date_terminated.setBounds(413, 83, 114, 14);
		}
		{
			jTextField_mobileIDD_start = new DateInputControl();
			tab2.add(jTextField_mobileIDD_start);
			jTextField_mobileIDD_start.setBounds(252, 97, 98, 21);
		}
		{
			jTextField_mobileData_start = new DateInputControl();
			tab2.add(jTextField_mobileData_start);
			jTextField_mobileData_start.setBounds(252, 122, 98, 21);
		}
		{
			jTextField_roaming_start = new DateInputControl();
			tab2.add(jTextField_roaming_start);
			jTextField_roaming_start.setBounds(252, 147, 98, 21);
		}
		{
			jTextField_mobileIDD_end = new DateInputControl();
			tab2.add(jTextField_mobileIDD_end);
			jTextField_mobileIDD_end.setBounds(412, 99, 96, 21);
		}
		{
			jTextField_mobileData_end = new DateInputControl();
			tab2.add(jTextField_mobileData_end);
			jTextField_mobileData_end.setBounds(412, 122, 96, 21);
		}
		{
			jTextField_roaming_end = new DateInputControl();
			tab2.add(jTextField_roaming_end);
			jTextField_roaming_end.setBounds(412, 147, 96, 21);
		}

		tabbedPane.setBounds(10, 25, 733, 228);
		panelSubscription.add(tabbedPane);
		
		JButton btnNew = new JButton("New");
		if(Common.USER_ROLE==UserRole.AGENT){
			btnNew.setEnabled(false);
		}
		btnNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					NewCustomer();
					NewSubscriptions();
				} catch (Exception e) {
					// TODO: handle exception
					Logger.log(e.getMessage());
				}
			}
		});
		btnNew.setBounds(10, 474, 89, 23);
		contentPane.add(btnNew);
		
		JButton btnSave = new JButton("Save");
		if(Common.USER_ROLE==UserRole.AGENT){
			btnSave.setEnabled(false);
		}
		
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if(IsValid()){
						BindObjects();
						TelephoneApplication ta = new TelephoneApplication();
						Logger.log("Customer before saving is :: "+mCustomer);
						ta.registerCustomer(mCustomer);
						
						//Added by tianyang start
						
						//Digital voice plan
						Plan dvPlan=getDVPlan();
						Plan mobilePlan=getMobilePlan();
						Plan cablePlan=getCablePlan();
						/*
						ArrayList <Plan> subscriptions = new ArrayList<Plan>();
						subscriptions.add(mDVPlan);
						subscriptions.add(mMobilePlan);
						subscriptions.add(mCablePlan);*/
						//TelephoneApplication telephoneApplication = new TelephoneApplication();
						//ta.subscriptions(mCustomer, subscriptions);
						
						Account account=new Account();
						account.setAccountNumber(mCustomer.getAccountNo());
						Logger.log("Dv plan : "+dvPlan);
						Logger.log("mobile plan : "+mMobilePlan);
						Logger.log("mcable plan : "+mCablePlan);
						
						account.addPlan(dvPlan);
						account.addPlan(mobilePlan);
						account.addPlan(cablePlan);
						ta.saveAccountForCustomer(mCustomer.getAccountNo(), account);
						
						Logger.log("Customer before account is :: "+mCustomer);
						Logger.log("Account before saving is :: "+account);
						
						JOptionPane.showMessageDialog(null,TelephonyConstants.MSG_SAVED_SUCCESSFULLY, "Info", JOptionPane.INFORMATION_MESSAGE);
						//Added by tianyang end
						
						
						/*if(mCustomer.SaveCustomer()==true){
							//show message here
						}*/
						for (Plan var : account.getPlans()) {
							BindPlan(var); //to show updated values or system assigned values to user
						}
					}
				} catch (Exception e) {
					// TODO: handle exception
					//Logger.log(e.getMessage());
					e.printStackTrace();
				}
			}
		});
		btnSave.setBounds(109, 474, 89, 23);
		contentPane.add(btnSave);
		
		JButton btnClose = new JButton("Close");
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
				try {
					this.finalize();
				} catch (Throwable e) {
					// TODO: handle exception
					Logger.log(e.getMessage());
				}
			};
		});
		btnClose.setBounds(208, 474, 89, 23);
		contentPane.add(btnClose);
		
		btnSearchCustomers = new JButton("Search Customers");
		btnSearchCustomers.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				FormAllCustomers formAllCustomers=new FormAllCustomers();
				getParent().add(formAllCustomers);
				Common.OpenCenterScreen(formAllCustomers, true);
			}
		});
		btnSearchCustomers.setBounds(604, 480, 143, 23);
		contentPane.add(btnSearchCustomers);
		
		btnViewComplaints = new JButton("View Complaints");
		btnViewComplaints.setBounds(449, 480, 147, 23);
		contentPane.add(btnViewComplaints);
		btnViewComplaints.addActionListener(new ComplaintsSelectActionListener());
	}
	
	private void NewCustomer(){	
		mCustomer=new Customer();		
		bindControls(mCustomer);
	}
	
	private void NewSubscriptions(){
		ClearSubscriptions();
	}
	private boolean IsValid(){
		if(txtNRIC.getText().trim().equals("")){
			JOptionPane.showMessageDialog(null,TelephonyConstants.MSG_KEYIN_NRIC, "Info", JOptionPane.INFORMATION_MESSAGE);
			txtNRIC.requestFocus();
			return false;
		}else if(txtAccountNo.getText().trim().equals("")){
			JOptionPane.showMessageDialog(null,TelephonyConstants.MSG_KEYIN_ACCTNO, "Info", JOptionPane.INFORMATION_MESSAGE);
			txtAccountNo.requestFocus();
			return false;
		}else if(txtName.getText().trim().equals("")){
			JOptionPane.showMessageDialog(null,TelephonyConstants.MSG_KEYIN_NAME, "Info", JOptionPane.INFORMATION_MESSAGE);
			txtName.requestFocus();
			return false;
		}else if(chkDVstatus.isSelected() && txtDVnumber.getText().trim().equals("")){
			JOptionPane.showMessageDialog(null,TelephonyConstants.MSG_KEYIN_DV_NUMBER, "Info", JOptionPane.INFORMATION_MESSAGE);
			txtDVnumber.requestFocus();
			return false;
		}else if(chkMVstatus.isSelected() && txtMVnumber.getText().trim().equals("")){
			JOptionPane.showMessageDialog(null,TelephonyConstants.MSG_KEYIN_MV_NUMBER, "Info", JOptionPane.INFORMATION_MESSAGE);
			txtMVnumber.requestFocus();
			return false;
		}else if(!IsValidEmail()){
			JOptionPane.showMessageDialog(null,TelephonyConstants.MSG_KEYIN_INVALID_EMAIL, "Info", JOptionPane.INFORMATION_MESSAGE);
			txtEmail.requestFocus();
			return false;
		}
		return true;
	}
	private boolean IsValidEmail(){			
		if(txtEmail.getText().trim().equals(""))return true;
		Pattern l_pattern=Pattern.compile(TelephonyConstants.EMAIL_PATTERN);
		return l_pattern.matcher(txtEmail.getText().trim()).matches();
	}
	
	private void ClearSubscriptions(){
			dteDVcommenced.setText("");
			dteDVterminated.setText("");
			txtDVnumber.setText("");
			chkDVstatus.setSelected(false);

				
			dteMVCommence.setText("");
			dteMVTerminated.setText("");
			txtMVnumber.setText("");
			chkMVstatus.setSelected(false);

			dteCTcommenced.setText("");
			dteCTterminated.setText("");
			chkCTstatus.setSelected(false);
		
			jTextField_voiceIDD_date_commenced.setText("");
			jTextField_voiceIDD_terminated.setText("");
			jCheckBox_Value_add_IDD.setSelected(false);

			jTextField_mobileIDD_start.setText("");
			jTextField_mobileIDD_end.setText("");
			jCheckBox_Mobile_IDD.setSelected(false);

			jTextField_callXfer.setText("");
			jTextField_callXsfer_terminated.setText("");
			jCheckBox_Call_transfer.setSelected(false);

			jTextField_mobileData_start.setText("");
			jTextField_mobileData_end.setText("");
			jCheckBox_Data_mobile.setSelected(false);

			jTextField_roaming_start.setText("");
			jTextField_roaming_end.setText("");
			jCheckBox_roaming.setSelected(false);
			
			jComboBox_TotalChannel2addModelModel.setSelectedItem(jComboBox_TotalChannel2addModelModel.getElementAt(0));
	}
	
	// added by tianyang start
	private void searchSubscription(Customer customer)  {
			Logger.log("Search Subscription start...");

			Logger.log(customer == null ? "Customer is null"
					: "Customer is not null");

			Logger.log(customer);

			Logger.log(customer.getAccountNo() == null ? "AccountNo is null"
							: "AcctNo is:" + customer.getAccountNo());
			if ((customer.getAccountNo() != null)&& !"".equals(customer.getAccountNo())) {
				Database database = DatabaseLoader.getInstance().getDatabase();
				Account tmpAccount = database.getAccountMap().get(customer.getAccountNo());
				Logger.log(tmpAccount == null ? "Account is null": "Account is not null");
				if (tmpAccount == null)
					return;

				Set<Plan> tmpPlanSet = tmpAccount.getPlans();
				Logger.log("Plan set : "+tmpPlanSet);
				Logger.log(tmpPlanSet == null ? "PlanSet is null"
						: "PlanSet is not null");
				for (Plan plan : tmpPlanSet) {
					BindPlan(plan);
				}
			}
	}
	
	private void BindPlan(Plan plan) {
			//Digital voice		
			if (PlanType.VOICE.equals(plan.getPlanType())){
				dteDVcommenced.setText(TelephonyUtils.getStringFromDate(plan.getDateCommenced()));
				dteDVterminated.setText(TelephonyUtils.getStringFromDate(plan.getDateTerminated()));
				txtDVnumber.setText(plan.getTelephoneNumber());
				if (Status.ACTIVE.toString().equals(plan.getSubscriptionStatus().toString())){
					chkDVstatus.setSelected(true);
				}else{
					chkDVstatus.setSelected(false);
				}
				mDVPlan=plan;
			}
			//Mobile
			if (PlanType.MOBILE.equals(plan.getPlanType())){
				dteMVCommence.setText(TelephonyUtils.getStringFromDate(plan.getDateCommenced()));
				dteMVTerminated.setText(TelephonyUtils.getStringFromDate(plan.getDateTerminated()));
				txtMVnumber.setText(plan.getTelephoneNumber());
				if (Status.ACTIVE.toString().equals(plan.getSubscriptionStatus().toString())){
					chkMVstatus.setSelected(true);
				}else{
					chkMVstatus.setSelected(false);
				}
				mMobilePlan = plan;
			}
			//Cable TV
			if (PlanType.CABLE.equals(plan.getPlanType())){
				dteCTcommenced.setText(TelephonyUtils.getStringFromDate(plan.getDateCommenced()));
				dteCTterminated.setText(TelephonyUtils.getStringFromDate(plan.getDateTerminated()));
				
				//jComboBox_TotalChannel2addModelModel.setSelectedItem(plan.getAdditionalChannels()); //FIXME Arun. Additional Channels are value added services. Not part of the plan 
				/*txtDVnumber.setText(plan.getTelephoneNumber());*/
				if (Status.ACTIVE.toString().equals(plan.getSubscriptionStatus().toString())){
					chkCTstatus.setSelected(true);
				}else{
					chkCTstatus.setSelected(false);
				}
				mCablePlan = plan;
			}
			
			Set <ValueAddedService> tmpValueAddedServiceSet = plan.getValueAddedServices();
			if (tmpValueAddedServiceSet != null) {
				for(ValueAddedService tmpValueAddedService: tmpValueAddedServiceSet){
					BindValueAddedService(plan, tmpValueAddedService);
				}
			}
		
	}
	
	private void BindValueAddedService(Plan plan, ValueAddedService valueAddedService) {
			if(ValueAddedType.IDD.equals(valueAddedService.getValueAddedType())){
				if(PlanType.VOICE.equals(plan.getPlanType())){
					jTextField_voiceIDD_date_commenced.setText(TelephonyUtils.getStringFromDate(valueAddedService.getDateCommenced()));
					jTextField_voiceIDD_terminated.setText(TelephonyUtils.getStringFromDate(valueAddedService.getDateTerminated()));
					if (Status.ACTIVE.equals(valueAddedService.getSubscriptionStatus())){
						jCheckBox_Value_add_IDD.setSelected(true);
					}else{
						jCheckBox_Value_add_IDD.setSelected(false);
					}
				}else if(PlanType.MOBILE.equals(plan.getPlanType())){
					jTextField_mobileIDD_start.setText(TelephonyUtils.getStringFromDate(valueAddedService.getDateCommenced()));
					jTextField_mobileIDD_end.setText(TelephonyUtils.getStringFromDate(valueAddedService.getDateTerminated()));
					if (Status.ACTIVE.equals(valueAddedService.getSubscriptionStatus())){
						jCheckBox_Mobile_IDD.setSelected(true);
					}else{
						jCheckBox_Mobile_IDD.setSelected(false);
					}
				}
			}

			if(ValueAddedType.CALLXFER.equals(valueAddedService.getValueAddedType())){
				jTextField_callXfer.setText(TelephonyUtils.getStringFromDate(valueAddedService.getDateCommenced()));
				jTextField_callXsfer_terminated.setText(TelephonyUtils.getStringFromDate(valueAddedService.getDateTerminated()));
				if (Status.ACTIVE.equals(valueAddedService.getSubscriptionStatus())){
					jCheckBox_Call_transfer.setSelected(true);
				}else{
					jCheckBox_Call_transfer.setSelected(false);
				}
			}
			
			if(ValueAddedType.DATA.equals(valueAddedService.getValueAddedType())){
				jTextField_mobileData_start.setText(TelephonyUtils.getStringFromDate(valueAddedService.getDateCommenced()));
				jTextField_mobileData_end.setText(TelephonyUtils.getStringFromDate(valueAddedService.getDateTerminated()));
				if (Status.ACTIVE.equals(valueAddedService.getSubscriptionStatus())){
					jCheckBox_Data_mobile.setSelected(true);
				}else{
					jCheckBox_Data_mobile.setSelected(false);
				}
			}
			
			if(ValueAddedType.ROAMING.equals(valueAddedService.getValueAddedType())){
				jTextField_roaming_start.setText(TelephonyUtils.getStringFromDate(valueAddedService.getDateCommenced()));
				jTextField_roaming_end.setText(TelephonyUtils.getStringFromDate(valueAddedService.getDateTerminated()));
				if (Status.ACTIVE.equals(valueAddedService.getSubscriptionStatus())){
					jCheckBox_roaming.setSelected(true);
				}else{
					jCheckBox_roaming.setSelected(false);
				}
			}
			
			if(ValueAddedType.CHANNEL.equals(valueAddedService.getValueAddedType())){
//				jTextField_roaming_start.setText(TelephonyUtils.getStringFromDate(valueAddedService.getDateCommenced()));
//				jTextField_roaming_end.setText(TelephonyUtils.getStringFromDate(valueAddedService.getDateTerminated()));
//				if (Status.ACTIVE.equals(valueAddedService.getSubscriptionStatus())){
//					jCheckBox_roaming.setSelected(true);
//				}else{
//					jCheckBox_roaming.setSelected(false);
//				}
				jComboBox_TotalChannel2addModelModel.setSelectedItem(valueAddedService.getAdditionalChannels()); 
			}
			
	}
	
	private Plan getDVPlan(){
		Plan mDVPlan=null;
		try{

			if(!"".equals(dteDVcommenced.getText()) || 
				!"".equals(txtDVnumber.getText()) || chkDVstatus.isSelected()){
					mDVPlan = new Plan();
					mDVPlan.setPlanId("P00001");
					mDVPlan.setPlanType(PlanType.VOICE);
					mDVPlan.setDateCommenced(TelephonyUtils.getSafeDate(dteDVcommenced.getText()));
					if(!dteDVterminated.getText().trim().equals(""))//only set terminated date when  there's
						mDVPlan.setDateTerminated(TelephonyUtils.getSafeDate(dteDVterminated.getText()));
					else
						mDVPlan.setDateTerminated(null);
					if(!chkDVstatus.isSelected())//if it's not active, have to set terminated date
						mDVPlan.setDateTerminated(TelephonyUtils.getSafeDate(dteDVterminated.getText()));
					mDVPlan.setTelephoneNumber(txtDVnumber.getText());
					mDVPlan.setSubscriptionStatus(chkDVstatus.isSelected()?Status.ACTIVE:Status.INACTIVE);
			}
			
			if(mDVPlan !=null){
				if(!"".equals(jTextField_voiceIDD_date_commenced.getText()) 
					|| jCheckBox_Value_add_IDD.isSelected()){
					
					ValueAddedService tmpValueAdded = new ValueAddedService();
					tmpValueAdded.setValueAddedId("V0001");
					//tmpValueAdded = mDVPlan.getValueAddedServiceObjectIfExists(tmpValueAdded);
					tmpValueAdded.setValueAddedType(ValueAddedType.IDD);
					
					mDVPlan.addValueAddedService(tmpValueAdded);
					
					tmpValueAdded.setDateCommenced(TelephonyUtils.getSafeDate(jTextField_voiceIDD_date_commenced.getText()));
					if(!jTextField_voiceIDD_terminated.getText().trim().equals(""))//only set terminated date when  there's
						tmpValueAdded.setDateTerminated(TelephonyUtils.getSafeDate(jTextField_voiceIDD_terminated.getText()));
					else
						tmpValueAdded.setDateTerminated(null);
					if(!jCheckBox_Value_add_IDD.isSelected())
						tmpValueAdded.setDateTerminated(TelephonyUtils.getSafeDate(jTextField_voiceIDD_terminated.getText()));
					tmpValueAdded.setSubscriptionStatus(jCheckBox_Value_add_IDD.isSelected()?Status.ACTIVE:Status.INACTIVE);
				}
				
				if(!"".equals(jTextField_callXfer.getText())
						|| jCheckBox_Call_transfer.isSelected()){
						
						ValueAddedService tmpValueAdded = new ValueAddedService();
						tmpValueAdded.setValueAddedId("V0002");
						//tmpValueAdded = mDVPlan.getValueAddedServiceObjectIfExists(tmpValueAdded);
						tmpValueAdded.setValueAddedType(ValueAddedType.CALLXFER);
						
						mDVPlan.addValueAddedService(tmpValueAdded);
						
						tmpValueAdded.setDateCommenced(TelephonyUtils.getSafeDate(jTextField_callXfer.getText()));
						if(!jTextField_callXsfer_terminated.getText().trim().equals(""))//only set terminated date when  there's
							tmpValueAdded.setDateTerminated(TelephonyUtils.getSafeDate(jTextField_callXsfer_terminated.getText()));
						else
							tmpValueAdded.setDateTerminated(null);
						if(!jCheckBox_Call_transfer.isSelected())
							tmpValueAdded.setDateTerminated(TelephonyUtils.getSafeDate(jTextField_callXsfer_terminated.getText()));
						tmpValueAdded.setSubscriptionStatus(jCheckBox_Call_transfer.isSelected()?Status.ACTIVE:Status.INACTIVE);
				}
			}
			
		}
		catch (Exception e){
			e.printStackTrace();
		}
		
		return mDVPlan;
	}
	
	private Plan getMobilePlan(){
		
		Plan mobilePlan=null;
		try{

			if(!"".equals(dteMVCommence.getText()) || 
				!"".equals(txtMVnumber.getText()) || chkMVstatus.isSelected()){
					mobilePlan = new Plan();
					mobilePlan.setPlanId("P00002");
					mobilePlan.setPlanType(PlanType.MOBILE);
					mobilePlan.setDateCommenced(TelephonyUtils.getSafeDate(dteMVCommence.getText()));
					if(!dteMVTerminated.getText().trim().equals(""))//only set terminated date when  there's
						mobilePlan.setDateTerminated(TelephonyUtils.getSafeDate(dteMVTerminated.getText()));
					else 
						mobilePlan.setDateTerminated(null);
					if(!chkMVstatus.isSelected())
						mobilePlan.setDateTerminated(TelephonyUtils.getSafeDate(dteMVTerminated.getText()));
					mobilePlan.setTelephoneNumber(txtMVnumber.getText());
					mobilePlan.setSubscriptionStatus(chkMVstatus.isSelected()?Status.ACTIVE:Status.INACTIVE);
			}
			
			if(mobilePlan !=null){
				if(!"".equals(jTextField_mobileIDD_start.getText()) || !"".equals(jTextField_mobileIDD_end.getText()) 
					|| jCheckBox_Mobile_IDD.isSelected()){
					
					ValueAddedService tmpValueAdded = new ValueAddedService();
					tmpValueAdded.setValueAddedId("V0003");
					//tmpValueAdded = mMobilePlan.getValueAddedServiceObjectIfExists(tmpValueAdded);
					tmpValueAdded.setValueAddedType(ValueAddedType.IDD);
					
					mobilePlan.addValueAddedService(tmpValueAdded);
					
					tmpValueAdded.setDateCommenced(TelephonyUtils.getSafeDate(jTextField_mobileIDD_start.getText()));
					if(!jTextField_mobileIDD_end.getText().trim().equals(""))//only set terminated date when  there's
						tmpValueAdded.setDateTerminated(TelephonyUtils.getSafeDate(jTextField_mobileIDD_end.getText()));
					else 
						tmpValueAdded.setDateTerminated(null);
					if(!jCheckBox_Mobile_IDD.isSelected())
						tmpValueAdded.setDateTerminated(TelephonyUtils.getSafeDate(jTextField_mobileIDD_end.getText()));
					tmpValueAdded.setSubscriptionStatus(jCheckBox_Mobile_IDD.isSelected()?Status.ACTIVE:Status.INACTIVE);
					
					
				}
				
				if(!"".equals(jTextField_mobileData_start.getText())
						|| jCheckBox_Data_mobile.isSelected()){
						
						ValueAddedService tmpValueAdded = new ValueAddedService();
						tmpValueAdded.setValueAddedId("V0004");
						//tmpValueAdded = mMobilePlan.getValueAddedServiceObjectIfExists(tmpValueAdded);
						tmpValueAdded.setValueAddedType(ValueAddedType.DATA);
						
						mobilePlan.addValueAddedService(tmpValueAdded);
						
						tmpValueAdded.setDateCommenced(TelephonyUtils.getSafeDate(jTextField_mobileData_start.getText()));
						if(!jTextField_mobileData_end.getText().trim().equals(""))//only set terminated date when  there's
							tmpValueAdded.setDateTerminated(TelephonyUtils.getSafeDate(jTextField_mobileData_end.getText()));
						else
							tmpValueAdded.setDateTerminated(null);
						if(!jCheckBox_Data_mobile.isSelected())
							tmpValueAdded.setDateTerminated(TelephonyUtils.getSafeDate(jTextField_mobileData_end.getText()));
						tmpValueAdded.setSubscriptionStatus(jCheckBox_Data_mobile.isSelected()?Status.ACTIVE:Status.INACTIVE);
				}
				
				if(!"".equals(jTextField_roaming_start.getText()) || !"".equals(jTextField_roaming_end.getText()) 
						|| jCheckBox_roaming.isSelected()){
						
						ValueAddedService tmpValueAdded = new ValueAddedService();
						tmpValueAdded.setValueAddedId("V0005");
						//tmpValueAdded = mMobilePlan.getValueAddedServiceObjectIfExists(tmpValueAdded);
						tmpValueAdded.setValueAddedType(ValueAddedType.ROAMING);
						
						mobilePlan.addValueAddedService(tmpValueAdded);
						
						tmpValueAdded.setDateCommenced(TelephonyUtils.getSafeDate(jTextField_roaming_start.getText()));
						if(!jTextField_roaming_end.getText().trim().equals(""))//only set terminated date when  there's
							tmpValueAdded.setDateTerminated(TelephonyUtils.getSafeDate(jTextField_roaming_end.getText()));
						else
							tmpValueAdded.setDateTerminated(null);
						if(!jCheckBox_roaming.isSelected())
							tmpValueAdded.setDateTerminated(TelephonyUtils.getSafeDate(jTextField_roaming_end.getText()));
						tmpValueAdded.setSubscriptionStatus(jCheckBox_roaming.isSelected()?Status.ACTIVE:Status.INACTIVE);
				}
			}
			
		}
		catch (Exception e){
			e.printStackTrace();
		}
		
		return mobilePlan;
	}
	
	private Plan getCablePlan(){
		Plan cablePlan=null;
		try{

			if(!"".equals(dteCTcommenced.getText()) || 
				chkCTstatus.isSelected()){
					cablePlan = new Plan();
					cablePlan.setPlanId("P00003");
					cablePlan.setPlanType(PlanType.CABLE);
					cablePlan.setDateCommenced(TelephonyUtils.getSafeDate(dteCTcommenced.getText()));
					if(!dteCTterminated.getText().trim().equals(""))//only set terminated date when  there's
						cablePlan.setDateTerminated(TelephonyUtils.getSafeDate(dteCTterminated.getText()));
					else
						cablePlan.setDateTerminated(null);
					if(!chkCTstatus.isSelected())
						cablePlan.setDateTerminated(TelephonyUtils.getSafeDate(dteCTterminated.getText()));
					cablePlan.setSubscriptionStatus(chkCTstatus.isSelected()?Status.ACTIVE:Status.INACTIVE);
			}
			
			if(cablePlan !=null){
				if(!"".equals(jComboBox_TotalChannel2addModelModel.getSelectedItem())){
					
					ValueAddedService tmpValueAdded = new ValueAddedService();
					tmpValueAdded.setValueAddedId("V0006");
					//tmpValueAdded = mCablePlan.getValueAddedServiceObjectIfExists(tmpValueAdded);
					tmpValueAdded.setValueAddedType(ValueAddedType.CHANNEL);
					
					cablePlan.addValueAddedService(tmpValueAdded);
					
					tmpValueAdded.setDateCommenced(TelephonyUtils.getSafeDate(dteCTcommenced.getText()));
					if(!dteCTterminated.getText().trim().equals(""))//only set terminated date when  there's
						tmpValueAdded.setDateTerminated(TelephonyUtils.getSafeDate(dteCTterminated.getText()));			
					else
						tmpValueAdded.setDateTerminated(null);
					tmpValueAdded.setAdditionalChannels(jComboBox_TotalChannel2addModelModel.getSelectedItem().toString());
					tmpValueAdded.setSubscriptionStatus("0".equals(jComboBox_TotalChannel2addModelModel.getSelectedItem())?Status.INACTIVE:Status.ACTIVE);
				}
			}
					
		}
		catch (Exception e){
			e.printStackTrace();
		}
		return cablePlan;
	}
	//added by tianyang end

	private Customer searchCustomer(String nric) throws CustomerNotFoundException {
		mCustomer =null;
		try {
			mCustomer = new Customer();
			Logger.log(nric);
			mCustomer = dao.retrieveCustomerData(nric.trim());
			Logger.log("Customer ... "+mCustomer);
			bindControls(mCustomer);
		} catch (CustomerNotFoundException e) {
			// TODO: handle exception
			throw e;
		}
		return mCustomer;
	}

	
	private void bindControls(Customer customer){
		txtName.setText(customer.getName());
		txtAddress.setText(customer.getAddress());
		txtEmail.setText(customer.getEmail());		
		txtNRIC.setText(customer.getNric());
		txtAccountNo.setText(customer.getAccountNo());
		txtInterests.setText(customer.getInterests());
		chkStatus.setSelected(customer.getStatus()==Status.ACTIVE);
	}
	private void BindObjects(){
		mCustomer.setAccountNo(txtAccountNo.getText());
		mCustomer.setName(txtName.getText());
		mCustomer.setAddress(txtAddress.getText());
		mCustomer.setEmail(txtEmail.getText());
		mCustomer.setNric(txtNRIC.getText());
		mCustomer.setInterests(txtInterests.getText());
		if(chkStatus.getSelectedObjects()==null) mCustomer.setStatus(Status.INACTIVE);
		else mCustomer.setStatus(Status.ACTIVE);
		
	}
	
	private class ComplaintsSelectActionListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent actionEvent) {

				 //JTable jtable = (JTable) mouseEvent.getSource();
			     
			     try {
					 String nric=mCustomer.getNric();
					 String name=mCustomer.getName();
					 
					 setVisible(false);
					 FormAllComplaints frmComplaintObj=new FormAllComplaints(nric,name );
					 getParent().add(frmComplaintObj);
					 Common.OpenCenterScreen(frmComplaintObj, true);
				} catch (Exception e) {
					JOptionPane.showMessageDialog(null, TelephonyConstants.ERR_SELECT_A_ROW, "Info", JOptionPane.INFORMATION_MESSAGE);
					//e.printStackTrace();
				}
		}
		
	}
	
	private class BillSelectActionListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent actionEvent) {

				 //JTable jtable = (JTable) mouseEvent.getSource();
			     
			     try {
					 					 
					 setVisible(false);
					 FormBillGenerator frmBillGenerator=new FormBillGenerator(mCustomer);
					 getParent().add(frmBillGenerator);
					 Common.OpenCenterScreen(frmBillGenerator, true);
				} catch (Exception e) {
					JOptionPane.showMessageDialog(null, TelephonyConstants.ERR_SELECT_A_ROW, "Info", JOptionPane.INFORMATION_MESSAGE);
					//e.printStackTrace();
				}
		}
		
	}
}
